﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using KGE;
using KGE.Define;

namespace NPKRipper
{
    public partial class Form1 : Form
    {
        byte[] fdata;

        string npk_name;
        NPK npk;
        KImagePackage kip;

        int pak_index = 0, img_index = 0;
        Point opoint;

        public Form1()
        {
            InitializeComponent();
        }

        private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "NPK File|*.npk|Kevin Image Package|*.kip";
            ofd.RestoreDirectory = true;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                openfile(ofd.FileName);

            }
        }

        public void openfile(string fn)
        {
            npk_name = new FileIndexer(fn).FolderName;
            gui_info.Text = npk_name;
            npk = null;
            kip = null;
            fdata = null;

            pak_index = img_index = -1;
            listBox1.Items.Clear();
            listBox2.Items.Clear();
 
            if (new FileIndexer(fn).ExtName.ToLower() == ".npk")
            {

                fdata = File.ReadAllBytes(fn);
                npk = new NPK(fdata);
                for (int i = 0; i < npk.GetPackageCount(); i++)
                {
                    listBox1.Items.Add(npk.PackageNames[i]);
                }
                pictureBox1.Refresh();
            }
            else if (new FileIndexer(fn).ExtName.ToLower() == ".kip")
            {


                kip = (KImagePackage)Util.LoadObject(fn);
                listBox1.Items.Add(kip.Name);

                pictureBox1.Refresh();
            }

            //KSound ks = new KSound("test.wav");
            //Util.SaveObject("test.KS", ks);
        }

        private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }



        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listBox1.SelectedIndex;
            if (i > -1)
            {
                pak_index = i;
                img_index = 0;

                listBox2.Items.Clear();
                if (npk != null)
                {
                    for (int j = 0; j < npk.GetImageCount(pak_index); j++)
                    {
                        listBox2.Items.Add((j + 0).ToString("d3"));
                    }
                }
                else
                {
                    for (int j = 0; j < kip.Count; j++)
                    {
                        listBox2.Items.Add(kip[j].Name);
                    }
                }
                pictureBox1.Refresh();
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = listBox2.SelectedIndex;
            if (i > -1)
            {
                img_index = i;
                update_gui();
                pictureBox1.Refresh();
            }
        }

        void update_gui()
        {
            int i = listBox1.SelectedIndex;
            int j = listBox2.SelectedIndex;

            if (i < 0 || j < 0)
            {
                return;
            }

            if (npk != null)
            {
                //textBox1.Text = "" + NImgF_I[i][j].size;          
                textBox2.Text = "" + npk.GetDataType(pak_index, img_index);
                textBox3.Text = "" + npk.GetDataFormat(pak_index, img_index);
                textBox4.Text = "" + npk.GetPackageSize(pak_index, img_index).Width;
                textBox5.Text = "" + npk.GetPackageSize(pak_index, img_index).Height;
                textBox6.Text = "" + npk.GetImageLocation(pak_index, img_index).X;
                textBox7.Text = "" + npk.GetImageLocation(pak_index, img_index).Y;
                textBox8.Text = "" + npk.GetImageSize(pak_index, img_index).Width;
                textBox9.Text = "" + npk.GetImageSize(pak_index, img_index).Height;
            }
            else
            {
                textBox6.Text = "" + kip[img_index].DrawRectangle.X;
                textBox7.Text = "" + kip[img_index].DrawRectangle.Y;
                textBox8.Text = "" + kip[img_index].DrawRectangle.Width;
                textBox9.Text = "" + kip[img_index].DrawRectangle.Height;
            }
        }

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (npk == null && kip == null)
            {
                return;
            }
            if (pak_index < 0 || img_index < 0)
            {
                return;
            }

            Graphics g = e.Graphics;

            g.Clear(Color.Gray);
            Image img;
            Point p;
            if (npk != null)
            {
                img = npk.GetImage(pak_index, img_index);
                p = npk.GetImageLocation(pak_index, img_index);
            }
            else
            {
                img = Image.FromStream(new MemoryStream(kip[img_index].Data));
                p = kip[img_index].DrawRectangle.Location;
            }
            if (img != null)
            {
                g.DrawImage(img, p);
                g.DrawString(opoint.ToString(), new Font("宋体", 16), Brushes.White, new PointF());

                Point p1 = opoint;
                Point p2 = opoint;
                p1.X -= 5;
                p2.X += 5;
                g.DrawLine(Pens.Red, p1, p2); 
                Point p3 = opoint;
                Point p4 = opoint;
                p3.Y -= 5;
                p4.Y += 5;
                g.DrawLine(Pens.Red, p3, p4);
            }
        }

        private void 保存SToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //if (npk == null)
            //{
            //    return;
            //}
            //SaveFileDialog sfd = new SaveFileDialog();
            //sfd.Filter = "Kevin Image Package|*.KIP";
            //if (sfd.ShowDialog() != DialogResult.OK)
            //{
            //    return;
            //}
            //if (sfd.FileName == string.Empty)
            //{
            //    return;
            //}
            if (kip != null)
            {
                SaveFileDialog sfd = new SaveFileDialog();
                sfd.RestoreDirectory = true;
                sfd.Filter = "Kevin Image|*.KI|PNG|*.PNG";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    //if (sfd.FileName != string.Empty)
                    {
                        if (sfd.FilterIndex == 1)
                        {
                            for (int i = 0; i < kip.Count; i++)
                            {
                                kip[i].Name = npk_name + "-" + kip.Name + "-" + i.ToString("d3");
                                Util.SaveObject(npk_name + "-" + kip.Name + "-" + i.ToString("d3") + ".KI", kip[i]);
                            }
                        }
                        else if (sfd.FilterIndex == 2)
                        {
                            for (int i = 0; i < kip.Count; i++)
                            {
                                File.WriteAllBytes(npk_name + "-" + kip.Name + "-" + i.ToString("d3") + ".PNG", kip[i].Data);
                            }
                        }
                    }
                }
            }
            if (npk != null)
            {
                KImagePackage[] kips = npk.ToKImagePackages();
                for (int i = 0; i < kips.Length; i++)
                {
                    //kips[i].Name = npk_name + "-" + kips[i].Name;
                    //for (int j = 0; j < kips[i].Count; j++)
                    //{
                    //    kips[i][j].Name = npk_name + "-" + kips[i][j].Name;
                    //}
                    Util.SaveObject(kips[i].Name + ".KIP", kips[i]);
                }
            }
            MessageBox.Show("保存成功");
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                opoint = e.Location;
                pictureBox1.Refresh();
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                //opoint = e.Location;
            }
        }

        private void 另存为AToolStripMenuItem_Click(object sender, EventArgs e)
        {


        }
    }
}
